/**
 * Storage 二次封装
 */
const namespace = 'manager';

export default {
  setItem(key, val) {
    let storage = this._getStorage();
    storage[key] = val;
    this._setStorage(storage);
  },
  // 支持 第二次参数传入一个默认值，获取不到的时候就会使用默认值
  getItem(key, defaultVal = '') {
    let storage = this._getStorage();
    return storage[key] || defaultVal;
  },
  clearItem(key) {
    let storage = this._getStorage();
    delete storage[key];
    this._setStorage(storage);
  },
  clearAll() {
    window.localStorage.clear();
  },
  // 内部获取全部的方法,保证最少返回一个 空对象
  _getStorage() {
    return JSON.parse(window.localStorage.getItem(namespace) || '{}');
  },
  _setStorage(storage) {
    window.localStorage.setItem(namespace, JSON.stringify(storage));
  },
};
